package com.jason.user.utils;

import sun.misc.BASE64Encoder;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {

    /**利用MD5进行加密*/
    public static String encryptByMD5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        //确定计算方法
        MessageDigest md5=MessageDigest.getInstance("MD5");
        BASE64Encoder base64en = new BASE64Encoder();
        //加密后的字符串
        return base64en.encode(md5.digest(str.getBytes("utf-8")));
    }

    /**判断用户密码是否正确
     *password 用户输入的密码
     *encryptPassword 正确密码
     */
    public static boolean authenticate(String password,String encryptPassword) throws NoSuchAlgorithmException, UnsupportedEncodingException{
        if(encryptByMD5(password).equals(encryptPassword))
            return true;
        else
            return false;
    }
}
